//
//  HWGameCircleRecommendCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/31.
//

import UIKit
import Cosmos
class HWGameCircleRecommendCell: UICollectionViewCell {
    var bgView:UIView!
    var logoImageView:UIImageView!
    var nameLabel:UILabel!
    var starImageView:UIImageView!
    var scoreLabel:UILabel!
    var typeLabel:UILabel!
    var rankLabel:UILabel!
    var recommendLabel:UILabel!
    var starView:CosmosView!
    var downloadLabel:UILabel!
    var model:HWGameCircleModel? {
        didSet {
            logoImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.name
            scoreLabel.text = model?.point
            typeLabel.text = model?.type
            if model?.size.count ?? 0 > 0 {
                rankLabel.text = " \(model?.size ?? "") "
            }
            starView.rating = model?.tuijianzhishu ?? 0
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCircleRecommendCell {
    private func initUI() -> Void {
    
        backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        
        bgView = UIView()
        contentView.addSubview(bgView)
        bgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 90*widthScale)
        UIColor.gradientColor(view: bgView, colors: [
            UIColor.colorWithAlphaHexString(hexString: "#FDF9EE", alpha: 1).cgColor,
            UIColor.colorWithAlphaHexString(hexString: "#FFFDF7", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
        
        logoImageView = UIImageView()
        logoImageView.backgroundColor = .clear
        logoImageView.layer.cornerRadius = 10*widthScale
        logoImageView.layer.masksToBounds = true
        bgView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 14*widthScale)
        bgView.addSubview(nameLabel)
        
        starImageView = UIImageView()
        starImageView.image = UIImage(named: "homeStar")
        bgView.addSubview(starImageView)
        
        scoreLabel = HWLabel.createLabel(title: "", color: "#444444", font: 10*widthScale)
        bgView.addSubview(scoreLabel)
        
        typeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 10*widthScale)
        bgView.addSubview(typeLabel)
        
        rankLabel = HWLabel.createLabel(title: "", color: "#953E3E", font: 10*widthScale)
        rankLabel.backgroundColor = .colorWithHexString(hexString: "#F7D0D0")
        rankLabel.layer.cornerRadius = 2*widthScale
        rankLabel.layer.masksToBounds = true
        bgView.addSubview(rankLabel)
        
        recommendLabel = HWLabel.createLabel(title: "推荐指数：", color: "#444444", font: 10*widthScale)
        bgView.addSubview(recommendLabel)
        
        starView = CosmosView.init()
        starView.rating = 0
        starView.settings.filledImage = UIImage(named: "fillStar")
        starView.settings.emptyImage = UIImage(named: "emptyStar")
        starView.settings.starSize = 11
        starView.settings.starMargin = 0
        starView.didFinishTouchingCosmos = {
            rating in

        }
        bgView.addSubview(starView)
        
        downloadLabel = HWLabel.createBoldLabel(title: "立即下载", color: "#F87706", font: 12*widthScale)
        downloadLabel.layer.cornerRadius = 4*widthScale
        downloadLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
        downloadLabel.layer.borderWidth = 1
        downloadLabel.textColor = .colorWithHexString(hexString: "#F87706")
        downloadLabel.textAlignment = .center
        downloadLabel.layer.masksToBounds = true
        bgView.addSubview(downloadLabel)
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.top.equalTo(10*widthScale)
        }
        
        logoImageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(15*widthScale)
            make.width.height.equalTo(60*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(11*widthScale)
            make.top.equalTo(21*widthScale)
        }
        
        starImageView.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(7*widthScale)
            make.left.equalTo(nameLabel)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.left.equalTo(starImageView.snp.right).offset(2*widthScale)
            make.centerY.equalTo(starImageView)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(scoreLabel.snp.right).offset(8*widthScale)
            make.centerY.equalTo(starImageView)
        }
        
        rankLabel.snp.makeConstraints { make in
            make.left.equalTo(typeLabel.snp.right).offset(8*widthScale)
            make.centerY.equalTo(starImageView)
            make.height.equalTo(12*widthScale)
        }
        
        recommendLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(starImageView.snp.bottom).offset(10*widthScale)
        }
        
        starView.snp.makeConstraints { make in
            make.centerY.equalTo(recommendLabel)
            make.left.equalTo(recommendLabel.snp.right)
        }
        
        downloadLabel.snp.makeConstraints { make in
            make.right.equalTo(-19*widthScale)
            make.centerY.equalToSuperview()
            make.width.equalTo(61*widthScale)
            make.height.equalTo(30*widthScale)
        }
    }
}
